import { mapGetters } from 'vuex'

var defaultImg = 'static/img/attestation_addition@3x.png'

var vm = {
    data() {
        return {
            cardFrontImg: defaultImg,
            cardBackImg: defaultImg,
            faceBestImg: defaultImg,
            cardIdConfirmShow: false,
            cardIdConfirmData: {}
        }
    },
    computed: {
        ...mapGetters(['userCardIdStatus'])
    },
    mounted() {
        if (this.userCardIdStatus) {
            $loading.show()
            $ajax.user.queryIdCard({
                userId: $store.state.user.userId
            }).then(res => {
                var { code, msg, data } = res.data
                if (code == 'S') {
                    this.cardBackImg = data.cardBackImg
                    this.cardFrontImg = data.cardFrontImg
                    this.faceBestImg = data.faceBestImg
                    $loading.hide()
                } else {
                    $toast.show(msg)
                }
            }).catch(err => {
                $loading.hide()
                $router.replace('/system/neterror')
            })
        }
    },
    methods: {
        faceOrcDo() {
            if (this.cardFrontImg == defaultImg) {
                $toast.show('请先拍摄身份证人像面照片')
            } else if (this.cardBackImg == defaultImg) {
                $toast.show('请先拍摄身份证国徽面照片')
            } else if (this.faceBestImg == defaultImg) {
                $toast.show('请先拍摄人脸照片')
            } else {
                $loading.show('上传中...')
                $ajax.user.faceOrc({
                    userId: $store.state.user.userId,
                    cardFrontImg: this.cardFrontImg,
                    cardBackImg: this.cardBackImg,
                    faceBestImg: this.faceBestImg
                }).then(res => {
                    var { code, msg, data } = res.data
                    if (code == 'S') {
                        $loading.hide()
                        this.cardIdConfirmData = data
                        this.cardIdConfirmShow = true
                    } else {
                        $toast.show(msg)
                    }
                }).catch(err => {
                    $toast.show('网络异常，请稍后重试')
                })
            }
        },
        reCardidDo() {
            // this.cardFrontImg = defaultImg
            // this.cardBackImg = defaultImg
            // this.faceBestImg = defaultImg
            // $toast.show('请重新拍摄，再次认证')
            this.cardIdConfirmShow = false
        },
        confirmCardidDo() {
            $loading.show()
            $ajax.user.faceVerity({
                userId: $store.state.user.userId
            }).then(res => {
                var { code, msg } = res.data
                $toast.show(msg)
                if (code == 'S') {
                    $store.dispatch('userInfoAjax')
                    $wxsdk.postLbs({
                        scene: 3,
                        mobile: $store.state.user.mobile,
                        userId: $store.state.user.userId
                    })
                    $router.replace('/loan/cacenter')
                }
            }).catch(err => {
                $toast.show('网络异常，请稍后重试')
            })
        }
    },
    mixins: [$mixins.needLogin]
}

export default vm
